﻿using HalconDotNet;
using System;
using System.Collections.Generic;

using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DetectApp
{
    /// <summary>
    /// CalibView.xaml 的交互逻辑
    /// </summary>
    public partial class CalibView : Window
    {
        public CalibView()
        {
            InitializeComponent();
        }

        private void BtnX5_Click(object sender, RoutedEventArgs e)
        {
            var file = $"CalibImage//X5.bmp";

            HImage hImage = new HImage(file);
            HalconFun.CalibX5(hImage, out HRegion ho_line1, out HRegion ho_line2, out HTuple hv_distance);
            mdisp.ClearAllROI();
            mdisp.ClearVisual();
            mdisp.Display(hImage);
         
            mdisp.DisplayRegion(ho_line1, Colors.Red);
            mdisp.DisplayRegion(ho_line2, Colors.Red);
            mdisp.DisplayText($"{(1000 / hv_distance.D):f3} um/pixel", Colors.Red, 50, new Point(50, 50));
            GlobalData.Instance.RatioParam.X5Ratio = Math.Round( 1000 / hv_distance.D,3);
        }

        private void BtnX10_Click(object sender, RoutedEventArgs e)
        {
            var file = $"CalibImage//X10.bmp";

            HImage hImage = new HImage(file);
            HalconFun.CalibX10(hImage, out HRegion ho_line1, out HRegion ho_line2, out HTuple hv_distance);
            mdisp.ClearAllROI();
            mdisp.ClearVisual();
            mdisp.Display(hImage);

            mdisp.DisplayRegion(ho_line1, Colors.Red);
            mdisp.DisplayRegion(ho_line2, Colors.Red);
            mdisp.DisplayText($"{(400 / hv_distance.D):f3} um/pixel", Colors.Red, 50, new Point(50, 50));
          GlobalData.Instance.RatioParam.X10Ratio= Math.Round(400 / hv_distance.D, 3);
        }

        private void BtnX20_Click(object sender, RoutedEventArgs e)
        {
            var file = $"CalibImage//X20.bmp";

            HImage hImage = new HImage(file);
            HalconFun.CalibX20(hImage, out HRegion ho_line1, out HRegion ho_line2, out HTuple hv_distance);
            mdisp.ClearAllROI();
            mdisp.ClearVisual();
            mdisp.Display(hImage);

            mdisp.DisplayRegion(ho_line1, Colors.Red);
            mdisp.DisplayRegion(ho_line2, Colors.Red);
            mdisp.DisplayText($"{(200 / hv_distance.D):f3} um/pixel", Colors.Red, 50, new Point(50, 50));
            GlobalData.Instance.RatioParam.X20Ratio = Math.Round(200 / hv_distance.D, 3);
        }

        private void BtnX50_Click(object sender, RoutedEventArgs e)
        {
            var file = $"CalibImage//X50.bmp";

            HImage hImage = new HImage(file);
            HalconFun.CalibX50(hImage, out HRegion ho_line1, out HRegion ho_line2, out HTuple hv_distance);
            mdisp.ClearAllROI();
            mdisp.ClearVisual();
            mdisp.Display(hImage);

            mdisp.DisplayRegion(ho_line1, Colors.Red);
            mdisp.DisplayRegion(ho_line2, Colors.Red);
            mdisp.DisplayText($"{(100 / hv_distance.D):f3} um/pixel", Colors.Red, 50, new Point(50, 50));
            GlobalData.Instance.RatioParam.X50Ratio = Math.Round(100 / hv_distance.D, 3);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            
                string filePath =System.IO. Path.Combine("Products", "ratioParam.json");
                if (System.IO. File.Exists(filePath)) System.IO.File.Delete(filePath);
                using System.IO.FileStream fileStream = new(filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
                JsonSerializer.Serialize(fileStream, GlobalData.Instance.RatioParam, new JsonSerializerOptions
                {
                    WriteIndented = true,
                    Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
                });
        }
    }
}
